package com.hnkywl.sts.service.taskdeal;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hnkywl.sts.dao.jhgl.QueryContractDao;
import com.hnkywl.sts.entity.xsgl.Dfkxsd;
import com.hnkywl.sts.service.xsgl.XsdService;
import net.ruixin.orm.hibernate.OrderBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 14-1-2
 * Time: 上午11:08
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DoQueryContract {

    private QueryContractDao queryContractDao;
    private XsdService xsdService;

    /**
     * 生成合同查询接口传递参数
     * @param count
     * @return
     * @throws Exception
     */
    public String generateArgsString(List<Dfkxsd> dfkxsdList,Integer count) throws Exception {
        Map result = new HashMap();
        String filter = "(order_id in ("+ net.ruixin.utils.CollectionUtils.fetchPropertyToString(dfkxsdList,"orderId",",") +")) and status >= 20";
        result.put("filter",filter);
        result.put("count",count.toString());
        return new Gson().toJson(result);
    }
    @Transactional
    public Map<String,List<Dfkxsd>> findDfkxsdList(){
        List<Dfkxsd> orderIdsOrderBySC = queryContractDao.findAll(new OrderBy[]{new OrderBy("salerCode", OrderBy.OrderType.ASC)});
        //将list转换为Map形式
        Map<String,List<Dfkxsd>> result = new HashMap<String, List<Dfkxsd>>();
        String salerCode = "";
        for(Dfkxsd item : orderIdsOrderBySC) {
            if(!salerCode.equals(item.getSalerCode())) {
                List<Dfkxsd> orderIds = new ArrayList<Dfkxsd>();
                salerCode = item.getSalerCode();
                orderIds.add(item);
                result.put(salerCode,orderIds);
            } else {
                result.get(salerCode).add(item);
            }
        }
        return result;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    public void deleteOrderId(Long id){
        Dfkxsd dfkxsd = queryContractDao.get(id);
        dfkxsd.setDeleted(1);
        dfkxsd.setLastUpdated(new Date());
        dfkxsd.setMessage("");
        queryContractDao.save(dfkxsd);
    }

    /**
     * 调用接口后业务处理方法
     * @param orderIdList
     * @param response
     */
    @Transactional
    public void dealByContractResponse(List<Dfkxsd> orderIdList,String response){
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        map = gson.fromJson(response, new TypeToken<Map<String, Object>>() {}.getType());
        //生成销售单
        if("0".equals(map.get("code"))) {
            xsdService.createXsdFromJson(response);
            //删除OrderId
//            deleteOrderIdList(orderIdList);
        } else {
            throw new RuntimeException(map.get("message").toString());
        }
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    public void deleteOrderIdList(List<Dfkxsd> orderIdList){
        for(Dfkxsd item : orderIdList){
            deleteOrderId(item.getId());
        }
    }

    @Transactional
    public void deleteOrderIdListWhenException(List<Dfkxsd> orderIdList,String exceptMessage){
        for(Dfkxsd item : orderIdList){
            Dfkxsd dfkxsd = queryContractDao.get(item.getId());
            dfkxsd.setMessage(exceptMessage);
            dfkxsd.setLastUpdated(new Date());
            queryContractDao.save(dfkxsd);
        }
    }

    @Autowired
    public void setQueryContractDao(QueryContractDao queryContractDao) {
        this.queryContractDao = queryContractDao;
    }
    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }
}
